<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 14-8-19
 * Time: 下午2:11
 */
namespace Common\Org\Upload;

class FileTypeBytesValid {
    static public function getExtension($bytesStream){
        switch(bin2hex(substr($bytesStream,0,2))){
            //jpg
            case 'ffd8' :
                $bool= 'ffd9' === bin2hex(substr($bytesStream,-2));
                $extension = '.jpg';
                break;
            //png
            case '8950' :
                $bool= '6082' === bin2hex(substr($bytesStream,-2));
                $extension = '.png';
                break;
            //gif
            case '4749' :
                $bool= '003b' === bin2hex(substr($bytesStream,-2));
                $extension = '.gif';
                break;

            //txt
            case '5345':
                $bool= '0a0a' === bin2hex(substr($bytesStream,-2));
                $extension = '.txt';
                break;

            //doc,xls
            case 'd0cf':
                $bool= '0000' === bin2hex(substr($bytesStream,-2));
                $extension = '.doc';
                break;

            //html
            case '3c68':
                $bool = '6c3e' === bin2hex(substr($bytesStream,-2));
                $extension = '.html';
                break;

            //xlsx,docx
            case '504b':
                $bool = '0000' === bin2hex(substr($bytesStream,-2));
                $extension = '.docx';
                break;

            //js
            case '7661':
                $bool = '0a7d' === bin2hex(substr($bytesStream,-2));
                $extension = '.js';
                break;

            //css
            case '4063':
                $bool = '227d' === bin2hex(substr($bytesStream,-2));
                $extension = '.css';
                break;

            //xml
            case '3c3f':
                $bool = '0d0a' === bin2hex(substr($bytesStream,-2));
                $extension = '.xml';
                break;

            default :
                $bool = false;
//                $extension = '';
        }

        if($bool){
            return array('size'=>strlen($bytesStream),'extension'=>$extension);
        } else {
            return -1;
        }
    }
} 